package com.xxq.web;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

import javax.servlet.ServletException;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;

import com.xxq.entity.Regist;

import my.mvc.MultipartRequest;
import my.mvc.RequestContext;

public class RegistAction {
	/**
	 * 增加学生或修改学生
	 * */
	public void save(RequestContext rc) throws ServletException, IOException {
		
		// 普通字段
		Regist regist= rc.form(Regist.class);
		
		// 文件字段
		MultipartRequest multiReq = (MultipartRequest)(rc.request());
		File file = multiReq.getFile("myfile");
		System.out.println(file.getName());
		InputStream is = new FileInputStream(file); 
		
		// 文件输出流
		String upload = rc.context().getRealPath("/upload");
		System.out.println(upload);
		String uuid = UUID.randomUUID().toString().replace("-", "");
		String fname = uuid + "." + FilenameUtils.getExtension(file.getName());
		FileOutputStream fos = new FileOutputStream(new File(upload, fname));
		
		// 将输入流写到输出流中
		IOUtils.copy(is, fos);
		is.close();
		fos.close();
		
		// 手动设置
		/*regist.setPicPath(fname);
		
		if(regist.getId() == 0) {
			registService.add(regist);
		} else {
			registService.update(regist);
		}
		*/
		// 重定向到一个查询请求
	//	rc.redirect(rc.contextPath() + "/action/student/list");
	}
	

	
}
